package com.gcp.ai.controller;

import com.gcp.ai.service.AiService;
import com.gcp.ai.service.SseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@RestController
@RequestMapping("/test")
public class TestController {

    private final AiService aiService;

    private final SseService sseService;

    @Autowired
    public TestController(AiService aiService, SseService sseService) {
        this.aiService = aiService;
        this.sseService = sseService;
    }

    @PostMapping("/simple")
    public String completion(@RequestBody String message) {
        System.out.println("接收到message: " + message);
        aiService.chat(message);
        return "收到";
    }

    @PostMapping("/img")
    public String getImg( @RequestBody String message) {
        return aiService.getImg(message);
    }

    @GetMapping("/audio")
    public String getAudio( @RequestParam(value = "audioPrompt", defaultValue = "AI时代下Java开发者该何去何从？")
                                      String audioPrompt) {
        aiService.getAudio(audioPrompt);
        return "success";
    }

    @GetMapping("/sse/{clientId}")
    public SseEmitter sseConnect(@PathVariable("clientId") String clientId){
        return sseService.createSseConnect(clientId);
    }

    @GetMapping("/sse/send/{clientId}/{msg}")
    public void sendMessage(@PathVariable("clientId") String clientId,@PathVariable("msg") String msg){
        sseService.sendMessage(clientId,msg);
    }

}
